<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ '▎ ' + showTitle }}</span>
      <span class="iconfont title-icon" :style="comStyle"
        ><i class="el-icon-arrow-down" @click="showChoice = !showChoice"></i
      ></span>
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div
          class="select-item"
          v-for="item in selectTypes"
          :key="item.code"
          @click="handleSelect(item.code)"
        >
          {{ item.fundName }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="fund_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
// import { getThemeValue } from '@/utils/theme_utils'
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      showChoice: false, // 是否显示可选项
      // choiceType: 'map', // 显示的数据类型
      titleFontSize: 0, // 指明标题文字的大小
      showTitle: '招商白酒',
      fundCode: '1',
      selectTypes: ''
    }
  },
  created() {
    // 在组件创建完成之后 进行回调函数的注册
    // this.$socket.registerCallBack('trendData', this.getData)
    this.getData()
    this.getGroupOfFund()
  },
  mounted() {
    this.initChart()
    // this.$socket.send({
    //   action: 'getData',
    //   socketType: 'trendData',
    //   chartName: 'trend',
    //   value: ''
    // })
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  watch: {
    theme() {
      console.log('主题切换了')
      this.chartInstane.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    // this.$socket.unRegisterCallBack('trendData')
  },
  computed: {
    ...mapState(['theme']),
    // selectTypes() {
    //   if (!this.allData) {
    //     return []
    //   } else {
    //     return this.allData.type.filter((item) => {
    //       return item.key !== this.choiceType
    //     })
    //   }
    // },
    // showTitle() {
    //   if (!this.allData) {
    //     return ''
    //   } else {
    //     return this.allData[this.choiceType].title
    //   }
    // },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + 'px',
        cursor: 'pointer'
      }
    }
  },
  methods: {
    initChart() {
      // this.chartInstane = this.$echarts.init(this.$refs.fund_ref, this.theme)
      this.chartInstane = this.$echarts.init(this.$refs.fund_ref)
      const initOption = {
        grid: {
          left: '3%',
          top: '35%',
          right: '4%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: 20,
          top: '20%',
          icon: 'circle'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          type: 'value'
        }
      }
      this.chartInstane.setOption(initOption)
    },
    async getData() {
      const userId = window.sessionStorage.getItem('token')
      const { data: res } = await this.$http.get(
        '/home/getFundStatistics?userId=' +
          userId +
          '&fundCode=' +
          this.fundCode
      )
      this.allData = res.data
      console.log(this.allData)
      this.updateChart()
    },

    async getGroupOfFund() {
      const userId = window.sessionStorage.getItem('token')
      const { data: res } = await this.$http.get(
        '/home/getGroupOfFund?userId=' + userId
      )
      if (res.code !== 1) {
        return this.$message.error('获取基金异常')
      }
      this.selectTypes = res.data
      console.log(this.selectTypes)
    },

    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // 处理数据
      // 类目轴的数据
      const timeArr = this.allData.xaxis
      // y轴的数据 series下的数据
      const valueArr = this.allData.yaxis
      console.log(timeArr)
      console.log(valueArr)
      // const seriesArr = valueArr.map((item, index) => {
      //   return {
      //     name: item.name,
      //     type: 'line',
      //     data: item.data,
      //     // stack: this.choiceType,
      //     areaStyle: {
      //       color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
      //         {
      //           offset: 0,
      //           color: colorArr1[index]
      //         }, // %0的颜色值
      //         {
      //           offset: 1,
      //           color: colorArr2[index]
      //         } // 100%的颜色值
      //       ])
      //     }
      //   }
      // })
      const seriesArr = {
        name: valueArr.fundName,
        type: 'line',
        data: valueArr.yvalues,
        smooth: true,
        // stack: this.choiceType,
        areaStyle: {
          color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              // color: colorArr1[index]
              color: colorArr1[1]
            }, // %0的颜色值
            {
              offset: 1,
              // color: colorArr2[index]
              color: colorArr2[1]
            } // 100%的颜色值
          ])
        },
        lineStyle: {
          normal: {
            color: '#16f2d9'
          }
        },
        itemStyle: {
          normal: {
            color: '#16f2d9'
          }
        }
      }
      // 图例的数据
      // const legendArr = valueArr.map((item) => {
      //   return item.name
      // })
      const legendArr = []
      legendArr.push(valueArr.fundName)
      const dataOption = {
        xAxis: {
          data: timeArr
        },
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.chartInstane.setOption(dataOption)
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.fund_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstane.setOption(adapterOption)
      this.chartInstane.resize()
    },
    handleSelect(currentType) {
      console.log(currentType)
      // this.choiceType = currentType
      this.showTitle = this.formatTitle(currentType)
      this.showChoice = false
      this.fundCode = currentType
      this.getData()
      this.updateChart()

      console.log(this.showTitle)
    },
    formatTitle(type) {
      if (type === 1) {
        return '招商白酒'
      } else if (type === 2) {
        return '广发'
      } else {
        return '其他'
      }
    }
  }
}
</script>
<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background-color: #222733;
  }
  .select-item {
    margin-top: 5px;
  }
}
</style>
